package com.jinzhi.jzweb.strategy.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.domain.ExaminationResultsComDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.domain.moduleConfig.DecryptDO;
import com.jinzhi.jzweb.pojo.moduleConfig.RecordPointsDO;
import com.jinzhi.jzweb.pojo.moduleConfig.SourVo;
import com.jinzhi.jzweb.pojo.moduleConfig.SourVoA;
import com.jinzhi.jzweb.service.ExaminationResultsComService;
import com.jinzhi.jzweb.service.StuSignUpComService;
import com.jinzhi.jzweb.service.moduleConfig.DecryptService;
import com.jinzhi.jzweb.strategy.BaseStrategy;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/*
 *  @author : JingChangTian
 *  @date   : 2023/8/11 20:20
 *  @description :  A赛场
 *
 */
@Component(value = "A")
public class ExaminationASour extends BaseController implements BaseStrategy  {


    private final ExaminationResultsComService examinationResultsComService;

    private final DecryptService decryptService;

    private final StuSignUpComService stuSignUpComService;


    public ExaminationASour(ExaminationResultsComService examinationResultsComService,
                            DecryptService decryptService,StuSignUpComService stuSignUpComService) {
        this.examinationResultsComService = examinationResultsComService;
        this.decryptService = decryptService;
        this.stuSignUpComService = stuSignUpComService;
    }


    @Override
    public void downloadImportTemplate(HttpServletResponse response,String field) {
        List<SourVo> asourVos = new ArrayList<>();
        //根据赛场查询成绩信息
        List<ExaminationResultsComDO> examinationResultsComDOS = examinationResultsComService.selectList(new EntityWrapper<ExaminationResultsComDO>()
                .eq("field",field));
        if(examinationResultsComDOS.isEmpty()){
            return;
        }
        examinationResultsComDOS.forEach(a ->{
            SourVo sourVo = new SourVo();
            sourVo.setScore1(a.getScore1());
            sourVo.setScore2(a.getScore2());
            sourVo.setScore3(a.getScore3());
            sourVo.setScore4(a.getScore4()==-1?"-": String.valueOf(a.getScore4()));
            sourVo.setScore5(a.getScore5()==-1?"-": String.valueOf(a.getScore5()));
            sourVo.setRemarkA(a.getRemarkA());
            sourVo.setNum2(a.getNum2());
            asourVos.add(sourVo);
        });
        //导出操作
        EasyPoiUtil.exportExcel(asourVos, "案例分析题选择二次加密与分数信息", "案例分析题选择二次加密与分数信息", SourVo.class, "案例分析题选择二次加密与分数信息", response);
    }

    @Override
    public void downloadImportTemplate2(HttpServletResponse response, String field,String type) {

    }

    @Override
    public void downloadImportBased(HttpServletResponse response, String field) {
        List<SourVoA> asourVos = new ArrayList<>();
        //根据赛场查询成绩信息
        List<ExaminationResultsComDO> examinationResultsComDOS = examinationResultsComService.selectList(new EntityWrapper<ExaminationResultsComDO>()
                .eq("field",field));
        if(examinationResultsComDOS.isEmpty()){
            return;
        }
        examinationResultsComDOS.forEach(a ->{
            SourVoA sourVo = new SourVoA();
            sourVo.setTotalScore(a.getTotalScore());
            sourVo.setNum2(a.getNum2());
            asourVos.add(sourVo);
        });
        //导出操作
        EasyPoiUtil.exportExcel(asourVos, "打分成绩导出", "打分成绩导出", SourVoA.class, "打分成绩导出", response);
    }

    @Transactional
    @Override
    public Result recordPoints(RecordPointsDO resultsComDO) {
        //根据赛场+二次加密验证是否有成绩信息
        List<ExaminationResultsComDO> examinationResultsComDOS = examinationResultsComService.selectList(new EntityWrapper<ExaminationResultsComDO>()
                .eq(StringUtils.isNotBlank(resultsComDO.getNum2()),"num2",resultsComDO.getNum2())
                .eq("field",resultsComDO.getField()));
        ExaminationResultsComDO resultsComDO1 = new ExaminationResultsComDO();
        //赋予分数

        resultsComDO1.setNum2(resultsComDO.getNum2());
        resultsComDO1.setField(resultsComDO.getField());
        resultsComDO1.setScore1(resultsComDO.getModuleOneMarkOne());
        resultsComDO1.setScore2(resultsComDO.getModuleOneMarkTwo());
        resultsComDO1.setScore3(resultsComDO.getModuleOneMarkThree());
        resultsComDO1.setScore4(null==resultsComDO.getModuleOneMarkFour()?-1:resultsComDO.getModuleOneMarkFour());
        resultsComDO1.setScore5(null==resultsComDO.getModuleOneMarkFive()?-1:resultsComDO.getModuleOneMarkFive());
        resultsComDO1.setRemarkA(StringUtil.isNull(resultsComDO.getRemark())?" ":resultsComDO.getRemark());
        //修改一二次加密状态为已录分
//        DecryptDO decryptDO = new DecryptDO();
//        decryptDO.setNum2s(resultsComDO.getNum2());
//        decryptDO.setField(resultsComDO.getField());
//        decryptDO  = decryptService.selectOne(decryptDO);
//        decryptDO.setStatus(2);
//        decryptService.updateById(decryptDO);

        StuSignUpComDO stuSignUpComDO = new StuSignUpComDO();
        stuSignUpComDO.setNum2(resultsComDO.getNum2());
        stuSignUpComDO.setCompetitionType(2);
        StuSignUpComDO stuSignUpComDO1 = stuSignUpComService.selectOne(stuSignUpComDO);
        stuSignUpComDO1.setIsShowJmxxppLl(1);
        stuSignUpComDO1.setStatus(2);
        stuSignUpComService.updateById(stuSignUpComDO1);

        if(examinationResultsComDOS.isEmpty()){
            //新增
            return Result.ok(examinationResultsComService.insert(resultsComDO1));
        }
        //修改
        return Result.ok(examinationResultsComService.update(resultsComDO1,new EntityWrapper<ExaminationResultsComDO>().eq("num2",resultsComDO.getNum2())
                .eq("field",resultsComDO.getField())));
    }


    @Transactional
    @Override
    public Result recordPoints2(RecordPointsDO resultsComDO) {
        //根据赛场+二次加密验证是否有成绩信息
        List<ExaminationResultsComDO> examinationResultsComDOS = examinationResultsComService.selectList(new EntityWrapper<ExaminationResultsComDO>()
                .eq(StringUtils.isNotBlank(resultsComDO.getNum2()),"num2",resultsComDO.getNum2())
                .eq("field",resultsComDO.getField()));
        ExaminationResultsComDO resultsComDO1 = new ExaminationResultsComDO();
        //赋予分数
        resultsComDO1.setNum2(resultsComDO.getNum2());
        resultsComDO1.setField(resultsComDO.getField());
        resultsComDO1.setScore1(resultsComDO.getModuleOneMarkOne());
        resultsComDO1.setScore2(resultsComDO.getModuleOneMarkTwo());
        resultsComDO1.setScore3(resultsComDO.getModuleOneMarkThree());
        resultsComDO1.setScore4(resultsComDO.getModuleOneMarkFour());
        resultsComDO1.setScore5(resultsComDO.getModuleOneMarkFive());
        resultsComDO1.setRemarkA(resultsComDO.getRemark());
        //修改一二次加密状态为已录分
//        DecryptDO decryptDO = new DecryptDO();
//        decryptDO.setNum2s(resultsComDO.getNum2());
//        decryptDO.setField(resultsComDO.getField());
//        decryptDO  = decryptService.selectOne(decryptDO);
//        decryptDO.setStatus(2);
//        decryptService.updateById(decryptDO);

        StuSignUpComDO stuSignUpComDO = new StuSignUpComDO();
        stuSignUpComDO.setNum2(resultsComDO.getNum2());
        stuSignUpComDO.setCompetitionType(2);
        StuSignUpComDO stuSignUpComDO1 = stuSignUpComService.selectOne(stuSignUpComDO);
        stuSignUpComDO1.setStatus(2);
        stuSignUpComService.updateById(stuSignUpComDO1);

        if(examinationResultsComDOS.isEmpty()){
            //新增
            return Result.ok(examinationResultsComService.insert(resultsComDO1));
        }
        //修改
        return Result.ok(examinationResultsComService.update(resultsComDO1,new EntityWrapper<ExaminationResultsComDO>().eq("num2",resultsComDO.getNum2())
                .eq("field",resultsComDO.getField())));
    }




    @Override
    public Result getScoreInfoPage(String field, Integer current, Integer size) {
        Page<ExaminationResultsComDO> page = new Page<ExaminationResultsComDO>(current,size);
        //查询已经匹配的解密列表
        Page<ExaminationResultsComDO> resultsComDOPage =
                examinationResultsComService.selectPage(page,new EntityWrapper<ExaminationResultsComDO>().eq("field",field));
        List<ExaminationResultsComDO> records = resultsComDOPage.getRecords();
        if(records!=null && !records.isEmpty()){
            for (ExaminationResultsComDO record : records) {
                if(record.getScore4()<0){
                    record.setScore4(null);
                }
                if(record.getScore5()<0){
                    record.setScore5(null);
                }
            }
        }
        return Result.ok(resultsComDOPage);
    }

    @Override
    public Result getScoreInfoPage2(String field, Integer current, Integer size, String type) {
        return null;
    }


}
